package com.qijian.common.utils;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.rmi.ServerException;
import java.util.*;
import java.util.stream.Collectors;

public class BeanValidatorUtil {
    private static final ValidatorFactory VALIDATOR_FACTORY = Validation.buildDefaultValidatorFactory();

    //返回map
    public static <T> List<String>  validate(int num, T t, Class... groups) {
        Validator validator = VALIDATOR_FACTORY.getValidator();
        Set validateResult = validator.validate(t, groups);
        //如果为空
        if (validateResult.isEmpty()) {
            return Collections.emptyList();
        } else {
            //不为空时表示有错误
            List<String> errors = new ArrayList<>();
            //遍历
            Iterator iterator = validateResult.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation violation = (ConstraintViolation) iterator.next();
                errors.add("第" + num + "行" + violation.getMessage());
            }
            return errors;
        }
    }

    public static <T> List<String> validate(T t, Class... groups) {
        Validator validator = VALIDATOR_FACTORY.getValidator();
        Set validateResult = validator.validate(t, groups);
        //如果为空
        if (validateResult.isEmpty()) {
            return Collections.emptyList();
        } else {
            //不为空时表示有错误
            List<String> errors = new ArrayList<>();
            //遍历
            Iterator iterator = validateResult.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation violation = (ConstraintViolation) iterator.next();
                errors.add(violation.getMessage());
            }
            return errors;
        }
    }

    //返回list
    public static List<String> validateList(Collection<?> collection) {
        //基础校验collection是否为空
        com.google.common.base.Preconditions.checkNotNull(collection);
        //遍历collection
        Iterator iterator = collection.iterator();
        List<String> errors = new ArrayList<>();
        int num = 1;
        while (iterator.hasNext()){
            Object object = iterator.next();
            errors.addAll(validate(num, object, new Class[0]));
            num++;
        }
        return errors;
    }

    // 校验某一对象是否合法
    public static List<String> validateObject(Object first, Object... objects) {
        if (objects != null && objects.length > 0) {
            return validateList(Lists.asList(first, objects));
        } else {
            return validate(first, new Class[0]);
        }
    }

    //校验参数方法
    public static void check(Object param) throws ServerException {
        List<String> error = BeanValidatorUtil.validateObject(param);
        //如果错误集合map不为空则抛出异常
        if (CollectionUtils.isNotEmpty(error)) {
            throw new ServerException(error.stream().collect(Collectors.joining(",")));
        }
    }
}
